package com.mach.platform.service.js.iot;

import com.mach.platform.util.SqlHelper;

import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class DevModel extends HashMap<Object,Object> {

    private final Map<String, Object> devInfoMap;
    private final ScriptEngine eng;
    private final SqlHelper sqlHelper;
    private final Integer devId;
    private final String sn;
    private final Map<Integer, Object> collMap;
    private final HashMap<String, Object> collNameKeyMap;
    private Integer type;
    public boolean isVirDev = true;

    public DevModel(Map<String, Object> devInfoMap , ScriptEngine eng, SqlHelper sqlHelper) {
        this.devInfoMap = devInfoMap;
        this.eng = eng;
        this.sqlHelper = sqlHelper;

        this.devId = Integer.parseInt(devInfoMap.get("devId").toString());
        this.sn = (String) devInfoMap.get("sn");
        this.type = (Integer) devInfoMap.get("type");

        if(null == type){
            this.type = new Integer(0);
        }
        if(type.equals(1) || type.equals(2)){ //虚拟表
            isVirDev = true;
        }else{
            isVirDev = false;
        }

        this.collMap = (Map<Integer, Object>) devInfoMap.get("coll");

        //collMap转为通过字段名作为key
        this.collNameKeyMap = new HashMap<String,Object>();
        Set<Entry<Integer, Object>> collEntrys = collMap.entrySet();
        for(Entry<Integer, Object> collEntry : collEntrys){
            Map<String, Object> coll = (Map<String, Object>) collEntry.getValue();
            String collName = (String)coll.get("collName");
            collNameKeyMap.put(collName, coll);
        }
    }


    @Override
    public Object get(Object key){
        String theTime = (String)this.eng.get("theTime");
        Boolean isNow = (Boolean)this.eng.get("isNow");
        Long pickCurTime = (Long)this.eng.get("pickCurTime");

        System.out.println("getData...sn="+ this.sn + " key="+ key); // debug
        Object result = null;

        Map<String, Object> coll = (Map<String, Object>)collNameKeyMap.get(key);
        if(coll == null){
            System.out.println("getData...sn="+ this.sn + " key="+ key + " errorCode='noCollName' info='无此类型，可能是功式错误'"); // debug
            return result;
        }



//        String collName = (String)coll.get("collName");
        String collFormula = (String)coll.get("collFormula");
        if(null == collFormula && this.isVirDev){
            return result;
        }

        StringBuilder scriptBd = new StringBuilder();
//        this.sqlHelper.query("select fo from iot_virtual_item where sn='"+ sn + "' and name='"+key+"'");
        scriptBd.append(collFormula);
        Object collResult = coll.get("formulaResult");
        try {

            if(null == collResult){
                System.out.println("getData...sn="+ this.sn + " key="+ key + " runFormula=" + collFormula + " isVir="+this.isVirDev); // debug

                if(this.isVirDev){
                    collResult = this.eng.eval(scriptBd.toString());
                }else{
                    String sqlGetValueFromDb = "";
                    if(isNow){
                        sqlGetValueFromDb = "select dvalue from iot_dev_cur_data_win  where dev_id="+this.devId
                                +" and data_type='"+key+"' and uptime>date_sub(now(), interval 1 HOUR) limit 1";
                    }else{
                        sqlGetValueFromDb = "select dvalue from iot_dev_data_win  where dev_id="+this.devId
                                +" and data_type='"+key+"' and the_time='"+theTime+"' limit 1";
                    }

                    Map<String, Object> dbResult = this.sqlHelper.queryOne(sqlGetValueFromDb);
                    collResult = dbResult.get("dvalue");

                }


                coll.put("formulaResult", collResult);
            }

        } catch (ScriptException e) {
            e.printStackTrace();
            collResult = null;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return collResult;
    }
}
